package com.cdtu.controller;

import com.cdtu.entity.SysMenu;
import com.cdtu.service.SysDeptService;
import com.cdtu.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author : Sakura
 * @Date : 2023/8/8 16:31
 * @Description : TODO 用来接收emp的请求
 */
@Controller
@RequestMapping("/menu")
//解决跨域问题
@CrossOrigin
public class SysMenuController extends BaseController{

    @Autowired
    private SysDeptService deptService;
    

    @Autowired
    private SysMenuService menuService;

    @RequestMapping("/list")
    public ModelAndView empList(ModelAndView modelAndView) {


        List<SysMenu> list = menuService.list();
        //返回数据
        modelAndView.addObject("menuList", list);
        //2.跳转页面;
        modelAndView.setViewName("system/sysMenu/menuList");

        return modelAndView;
    }

    @RequestMapping("/delete/{menuId}")
    public ModelAndView delete(@PathVariable("menuId") Long menuId, ModelAndView modelAndView) {

        boolean remove = menuService.removeById(menuId);
        modelAndView.setViewName("redirect:/menu/list");
        return modelAndView;
    }

    @RequestMapping("/addMenu")
    public ModelAndView addMenu(SysMenu SysMenu, ModelAndView modelAndView) {

        if (SysMenu.getfMenuId() == 1) {
            SysMenu.setType(1);
        }else {
            SysMenu.setType(2);
        }
        menuService.save(SysMenu);
        //2.跳转页面;
        modelAndView.setViewName("redirect:/menu/list");
        return modelAndView;
    }

    @RequestMapping("/toUpdateUI/{menuId}")
    public ModelAndView toUpdateUi(ModelAndView modelAndView,@PathVariable("menuId") Long menuId) {

        SysMenu sysMenu = menuService.getById(menuId);
        //返回数据
        modelAndView.addObject("sysMenu", sysMenu);


        List<SysMenu> list = menuService.list();
        modelAndView.addObject("menuList", list);

        //2.跳转页面;
        modelAndView.setViewName("system/sysMenu/updateSysMenu");
        return modelAndView;
    }

    @RequestMapping("/toAddUI")
    public ModelAndView toAddSysMenu(ModelAndView modelAndView) {
        //2.跳转页面;

        List<SysMenu> list = menuService.list();
        modelAndView.addObject("menuList", list);
        modelAndView.setViewName("system/sysMenu/addSysMenu");
        return modelAndView;
    }


    @RequestMapping("/update")
    public ModelAndView update(ModelAndView modelAndView,SysMenu SysMenu) {
        if (SysMenu.getfMenuId() == 1) {
            SysMenu.setType(1);
        }else {
            SysMenu.setType(2);
        }
        boolean update = menuService.updateById(SysMenu);
        //2.跳转页面;
        modelAndView.setViewName("redirect:/menu/list");
        return modelAndView;
    }
}
